package com.i_art_engineer.meigong.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.activity.MainActivity.AdInfo;
import com.i_art_engineer.meigong.gallery.GalleryView;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Set;

public class FlowAdAdapter extends BaseAdapter {

    private ArrayList<ImageView> mViews = new ArrayList<>();        // 保存倒影图片的数组
   private  ArrayList<ImageView>tmpViews=new ArrayList<>();
    private Context mContext;

    public FlowAdAdapter(Context c) {
        this.mContext = c;
        initView();
    }
    public void initView()
    {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        final int Height = wm.getDefaultDisplay().getHeight() * 5 / 12;


        mViews.clear();
        Bitmap originalImage = readBitMap(mContext,R.drawable.flow_ad_default);
        for (int i = 0; i < 3; i++) {


            int width = originalImage.getWidth();
            int height = originalImage.getHeight();
            float scale = Height / (float) height;

            Matrix sMatrix = new Matrix();
            sMatrix.postScale(scale, scale);
            Bitmap miniBitmap = Bitmap.createBitmap(originalImage, 0, 0,
                    originalImage.getWidth(), originalImage.getHeight(), sMatrix, true);



            int mwidth = miniBitmap.getWidth();
            int mheight = miniBitmap.getHeight();

            ImageView imageView = new ImageView(mContext);
            imageView.setImageBitmap(miniBitmap);
            imageView.setLayoutParams(new GalleryView.LayoutParams(
                    mwidth,
                    mheight
            ));
            imageView.setScaleType(ScaleType.FIT_XY);
            mViews.add(imageView);
//            Picasso.with(mContext)
//                    .load(R.drawable.flow_ad_default)
//                    .resize(mwidth, mheight)
//                    .memoryPolicy(MemoryPolicy.NO_STORE)
//                    .into(imageView);
        }
        //是否原图片数据，节省内存
        originalImage.recycle();
    }
    /**
     * 释放多余的View
     *
     * @param size
     */
    private void adjustSize(int size) {
        if (size < mViews.size()) {
            for (int i = size; i < mViews.size(); i++)
                mViews.remove(i);
        }
    }
    public static Bitmap readBitMap(Context context, int resId){
            BitmapFactory.Options opt = new BitmapFactory.Options();
           opt.inPreferredConfig = Bitmap.Config.RGB_565;
           opt.inPurgeable = true;
             opt.inInputShareable = true;
               //获取资源图片
             InputStream is = context.getResources().openRawResource(resId);
                 return BitmapFactory.decodeStream(is,null,opt);
         }
    /**
     * 创建所有视图
     */
    public boolean buildViews(ArrayList<String> urls, ArrayList<AdInfo> data) {

        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        final int Height = wm.getDefaultDisplay().getHeight() * 5 / 12;

//        adjustSize(urls.size());
        for(int i=0;i<mViews.size();i++)
        {
            tmpViews.add(mViews.get(i));
        }
        FreeTmpBitmap();
        mViews.clear();
        for (int i = 0; i < urls.size(); i++) {
            AdInfo ad = data.get(i);

            // 获取原始图片
//            Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.flow_ad_default);
            Bitmap originalImage = readBitMap(mContext,R.drawable.flow_ad_default);

            int width = originalImage.getWidth();
            int height = originalImage.getHeight();
            float scale = Height / (float) height;

            Matrix sMatrix = new Matrix();
            sMatrix.postScale(scale, scale);
            Bitmap miniBitmap = Bitmap.createBitmap(originalImage, 0, 0,
                    originalImage.getWidth(), originalImage.getHeight(), sMatrix, true);

            //是否原图片数据，节省内存
            originalImage.recycle();

            int mwidth = miniBitmap.getWidth();
            int mheight = miniBitmap.getHeight();

            ImageView imageView = new ImageView(mContext);

            imageView.setImageBitmap(miniBitmap);
            imageView.setLayoutParams(new GalleryView.LayoutParams(
                    mwidth,
                    mheight
            ));
            imageView.setTag(ad);
            imageView.setScaleType(ScaleType.FIT_XY);
            mViews.add(imageView);
//            Picasso.with(mContext)
//                    .load(urls.get(i))
//                    .resize(mwidth, mheight)
//                    .memoryPolicy(MemoryPolicy.NO_STORE)
//                    .into(imageView);
        }
        return true;
    }

    public void FreeTmpBitmap()
    {
        if(tmpViews!=null&&tmpViews.size()>0)
        {
            for(int i=0;i<tmpViews.size();i++) {
                ImageView iv = tmpViews.get(i);
                Drawable bd= iv.getDrawable();
                if(bd!=null) {
                    Bitmap bitmap =((BitmapDrawable) bd).getBitmap();
                    if (bitmap != null && !bitmap.isRecycled()) {
                        bitmap.recycle();
                        bitmap = null;
                    }
                }
            }
        }
    }
    public void FreeBitmap()
    {
        if(mViews!=null&&mViews.size()>0)
        {
            for(int i=0;i<mViews.size();i++) {
                ImageView iv = mViews.get(i);
                Drawable bd= iv.getDrawable();
                if(bd!=null) {
                    Bitmap bitmap =((BitmapDrawable) bd).getBitmap();
                    if (bitmap != null && !bitmap.isRecycled()) {
                        bitmap.recycle();
                        bitmap = null;
                    }
                }
            }
        }
    }

    @Override
    public int getCount() {
//        return mViews.size();
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
        return mViews.get(position%mViews.size());
    }

    @Override
    public long getItemId(int position) {
        return position%mViews.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(mViews.size()==0)
            return null;
        else
          return mViews.get(position%mViews.size());
    }

}